
from sqlalchemy import Column, Integer, String, Text
from app.entity.baseModal import BaseModal
from sqlalchemy.orm import Mapped,mapped_column,declarative_base, relationship,sessionmaker

class ProjectUser(BaseModal):
    __tablename__="jz_project_users"
    _idGeneratorType="db"
    _fieldIgnore = []
    projectId:Mapped[int]=mapped_column(String(100),name="project_id")
    userId:Mapped[int]=mapped_column(String(255),name="user_id")
    isAdmin:Mapped[str]=mapped_column(String(1),name="is_admin")
    defalutGroupId:Mapped[int]=mapped_column(Integer(),name="defalut_group_id")

    def getDefalutGroupIdDescAttr(self):
        from app.entity.projectGroup import ProjectGroup
        pg=ProjectGroup().query().filter(ProjectGroup.id==self.defalutGroupId).with_entities(ProjectGroup.name).first()
        return pg.name if pg!=None else ""
    
    def getUserInfoAttr(self):
        from app.entity.user import User
        user=User().query().filter(User.id==self.userId).with_entities(User.id,User.nickname,User.avatarUrl).first()
        json={}
        if user!=None:
            json={
                "id":user.id,
                "nickname":user.nickname,
                "avatarUrl": user.avatarUrl
            }
        return json
    
    def getUser(self):
        from app.entity.user import User
        user=User().query().filter(User.id==self.userId).first()
        return user